//
//  SJRefreshMyHead.m
//  Recruit
//
//  Created by 张艳江 on 2019/7/9.
//  Copyright © 2019 陈晓晶. All rights reserved.
//

#import "SCRefreshMyHeader.h"
#import "NSBundle+MJRefresh.h"

@interface SCRefreshMyHeader()

@property (weak, nonatomic) UIActivityIndicatorView *loadingView;

@end

@implementation SCRefreshMyHeader

- (UIActivityIndicatorView *)loadingView
{
    if (!_loadingView) {
        UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:_activityIndicatorViewStyle];
        loadingView.hidesWhenStopped = NO;
        [self addSubview:_loadingView = loadingView];
    }
    return _loadingView;
}
#pragma mark - 公共方法
- (void)setActivityIndicatorViewStyle:(UIActivityIndicatorViewStyle)activityIndicatorViewStyle
{
    _activityIndicatorViewStyle = activityIndicatorViewStyle;
    
    self.loadingView = nil;
    [self setNeedsLayout];
}

#pragma mark - 重写父类的方法
- (void)prepare
{
    [super prepare];
    
    _activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    
    //隐藏时间
    self.lastUpdatedTimeLabel.hidden = YES;//设置时间显示的话会出现在左边动画
    //隐藏状态
    self.stateLabel.hidden = YES;//隐藏动态标签
}
- (void)placeSubviews
{
    [super placeSubviews];
    // 圈圈
    self.loadingView.center = CGPointMake(self.mj_w * 0.5, self.mj_h * 0.5);
}

- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState
    // 根据状态做事情
    if (state == MJRefreshStateIdle) {
        if (oldState == MJRefreshStateRefreshing) {
            
            [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
                //self.loadingView.alpha = 0.0;
            } completion:^(BOOL finished) {
                // 如果执行完动画发现不是idle状态，就直接返回，进入其他状态
                if (self.state != MJRefreshStateIdle) return;
                
                //self.loadingView.alpha = 1.0;
                [self.loadingView stopAnimating];
            }];
        } else {
            [self.loadingView stopAnimating];
        }
    } else if (state == MJRefreshStatePulling) {
        [self.loadingView stopAnimating];
    } else if (state == MJRefreshStateRefreshing) {
        //self.loadingView.alpha = 1.0; // 防止refreshing -> idle的动画完毕动作没有被执行
        [self.loadingView startAnimating];
    }
}


@end
